package com.example.srpingsecurityoauth2.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.srpingsecurityoauth2.mapper.UserMapper;
import com.example.srpingsecurityoauth2.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @ClassName UserService
 * @Description
 * @Author gqz
 * @Date 2021/12/30 15:38
 * @Version 1.0
 **/
@Service
public class UserService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("开始执行登录逻辑");
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        if (user!=null){
            return new User(user.getUsername(),user.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
        }
        return null;
    }
}
